package com.demo.controller;

import com.demo.pojo.Dept;
import com.demo.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chenyuan
 * @date 2020/9/28 18:04
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @PostMapping("/addDept")
    public Boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/queryDeptById/{id}")
    public Dept queryDeptById(@PathVariable("id") Long id){
        return deptService.queryDeptById(id);
    }

    @GetMapping("/queryDepts")
    public List<Dept> queryDepts() {
        return deptService.queryDepts();
    }

    @Autowired
    private DiscoveryClient client;
    /**
     * 获取注册进来的微服务的信息
     */
    @GetMapping("/provider/discover")
    public Object discover(){
        // 获取微服务列表清单
        List<String> services = client.getServices();
        System.out.println("discover - Services : " + services);

        // 得到一个具体的微服务,参数：微服务的实例 ID
        List<ServiceInstance> instances = client.getInstances("SpringCloud-02-provider");
        System.out.println("====instances===");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getServiceId());
        }
        return this.client;
    }
}
